package com.symaster.common.swing.dialog;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.symaster.common.config.Config;
import com.symaster.common.event.DefaultEventResult;
import com.symaster.common.event.EventManger;
import com.symaster.common.event.EventResult;
import com.symaster.common.swing.BaseDialog;
import com.symaster.common.swing.element.CardListPanel;
import com.symaster.common.swing.entity.ConfigSettingDialogItem;
import com.symaster.common.swing.entity.ConfigSettingDialogParam;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE;

public class ConfigSettingDialog extends BaseDialog {
    private Config config;
    private Map<String, String> annotation;
    private JPanel contentPane;
    private JButton ok_button;
    private JButton cancel_button;
    private JButton reload_button;
    private JPanel center_panel;
    private CardListPanel cardListPanel;
    private int updateFlag = 0;

    public static void main(String[] args) {
        Config config1 = new Config("abc.config");
        config1.put("testKey", "testValue");
        config1.put("testKe2y", "testValue");
        config1.put("testKe3y", "testValue");
        config1.put("testKe4y", "testValue");
        config1.save();

        HashMap<String, String> likeNameMap = new HashMap<>();
        likeNameMap.put("testKey", "测试1");
        likeNameMap.put("testKe2y", "测试2");
        likeNameMap.put("testKe3y", "测试3");
        likeNameMap.put("testKe4y", "测试4");

        ConfigSettingDialog dialog = new ConfigSettingDialog();

        EventResult configSettingDialog = EventManger.eventOccurs("ConfigSettingDialog", config1, null, likeNameMap);
        DefaultEventResult success = configSettingDialog.success;

        if (success.getResultData() instanceof Integer && ((Integer) success.getResultData()) == 1) {
            config1.save();
        }
        System.exit(0);
    }

    private void reload() {
        cardListPanel.listRemoveAll();
        for (String key : new TreeSet<>(config.keySet())) {
            String likeName = annotation != null ? annotation.get(key) : null;
            cardListPanel.listAdd(new ConfigSettingDialogItem(key, likeName, config.get(key)));
        }
    }

    private void onOK() {
        Component[] components = cardListPanel.listComponents();
        for (Component item : components) {
            if (item instanceof ConfigSettingDialogItem) {
                ConfigSettingDialogItem configSettingDialogItem1 = (ConfigSettingDialogItem) item;
                config.put(configSettingDialogItem1.getKey(), configSettingDialogItem1.getValue());
            }
        }
        updateFlag = 1;
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    @Override
    public String listenerKey() {
        return "ConfigSettingDialog";
    }

    @Override
    public void initPage() {
        cardListPanel = new CardListPanel(-1, 28, 5, 0);
        center_panel.setLayout(new BorderLayout());
        center_panel.add(cardListPanel, BorderLayout.CENTER);
        pack();
    }

    @Override
    public Dimension pageSize() {
        return new Dimension(900, 600);
    }

    /**
     * 初始化数据
     *
     * @param param param[0] com.symaster.common.config.Config 配置类,不能为空
     *              param[1] java.awt.Frame 用于设置icon,可以为空
     *              param[2] Map<String, String> 可以为空
     */
    @Override
    public void initData(Object... param) {
        if (param == null || param.length == 0) {
            throw new IllegalArgumentException();
        }
        if (param[0] == null || !(param[0] instanceof ConfigSettingDialogParam)) {
            throw new IllegalArgumentException();
        }
        ConfigSettingDialogParam configSettingDialogParam = (ConfigSettingDialogParam) param[0];

        if (configSettingDialogParam.getConfig() == null) {
            throw new IllegalArgumentException("ConfigSettingDialogParam.config is Null");
        } else {
            this.config = configSettingDialogParam.getConfig();
        }

        if (configSettingDialogParam.getFrame() != null) {
            setIconImage(configSettingDialogParam.getFrame().getIconImage());
        }

        if (configSettingDialogParam.getAnnotation() != null) {
            annotation = configSettingDialogParam.getAnnotation();
        }

        if (configSettingDialogParam.getTitle() != null) {
            setTitle(configSettingDialogParam.getTitle());
        }

        this.reload();
    }

    @Override
    public Object result() {
        return updateFlag;
    }

    @Override
    public Container rootContainer() {
        return contentPane;
    }

    @Override
    public void initListener() {
        // setModal(true);
        // getRootPane().setDefaultButton(ok_button);
        ok_button.addActionListener(e -> onOK());
        cancel_button.addActionListener(e -> onCancel());
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
        // contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        reload_button.addActionListener(e -> reload());
    }

    @Override
    public void initText() {

    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel1.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 3, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        ok_button = new JButton();
        ok_button.setText("OK");
        panel2.add(ok_button, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        cancel_button = new JButton();
        cancel_button.setText("Cancel");
        panel2.add(cancel_button, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        reload_button = new JButton();
        reload_button.setText("reload");
        panel2.add(reload_button, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        center_panel = new JPanel();
        center_panel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(center_panel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }

}

